package com.fin.zw.intelligentbot.controller;

import com.fin.zw.intelligentbot.annotation.CurrentUser;
import com.fin.zw.intelligentbot.model.request.RobotInfoRequest;
import com.fin.zw.intelligentbot.model.request.UserIds;
import com.fin.zw.intelligentbot.model.response.RobotInfoResponse;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.service.RobotInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/robot")
@Api(value = "机器人管理", description = "机器人管理")
public class RobotInfoController {

    @Autowired
    private RobotInfoService robotInfoService;

    @GetMapping("/fetch")
    @ApiOperation(value = "查询所有的机器人信息", notes = "查询所有的机器人信息")
    public ResponseEntity<List<RobotInfoResponse>> fetch(RobotInfoRequest request,
                                                         @ApiIgnore @CurrentUser SessionModel sessionModel) {
        List<RobotInfoResponse> robots = robotInfoService.fetch(request, sessionModel);
        return ResponseEntity.ok().body(robots);
    }


    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询所有的机器人信息", notes = "查询所有的机器人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<RobotInfoResponse>> queryRobotInfoAll(Pageable pageable,
                                                                     RobotInfoRequest request,
                                                                     @ApiIgnore @CurrentUser SessionModel sessionModel) {
        Page<RobotInfoResponse> page = robotInfoService.queryRobotInfoAll(pageable, request, sessionModel);
        return ResponseEntity.ok().body(page);
    }


    @PostMapping("/add")
    @ApiOperation(value = "机器人添加", notes = "机器人添加")
    public ResponseEntity<String> createRobotInfo(@RequestBody RobotInfoRequest request,
                                                  @ApiIgnore @CurrentUser SessionModel sessionModel) {
        robotInfoService.createRobotInfo(request, sessionModel);
        return ResponseEntity.ok().body("添加成功");
    }

    @RequestMapping(value = "/update", method = {RequestMethod.PUT, RequestMethod.PATCH})
    @ApiOperation(value = "机器人更新", notes = "机器人更新")
    public ResponseEntity<String> updateRobotInfo(@RequestBody RobotInfoRequest request,
                                                  @ApiIgnore @CurrentUser SessionModel sessionModel) {
        robotInfoService.updateRobotInfo(request, sessionModel);
        return ResponseEntity.ok().body("更新成功");
    }

    @PostMapping("/recoveryRobot")
    @ApiOperation(value = "机器人回收", notes = "机器人回收")
    public ResponseEntity<String> recoveryRobot(@RequestBody UserIds request) {
        robotInfoService.recoveryRobot(request.getIds());
        return ResponseEntity.ok().body("回收成功");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "机器人删除", notes = "机器人删除")
    public ResponseEntity<String> deleteRobotById(@PathVariable String id,
                                                  @ApiIgnore @CurrentUser SessionModel sessionModel) {
        robotInfoService.deleteRobotInfoById(id, sessionModel);
        return ResponseEntity.ok().body("删除成功");
    }
}
